package ecustom.common;

import org.apache.log4j.Logger;

import ecustom.common.util.CustomUtil;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

public abstract class AbstractAction extends AbstractBaseAction implements Action {

	protected Logger log = Logger.getLogger(this.getClass());

	@Override
	public String execute(RequestInfo requestInfo) {
		int requestId = requestInfo.getRequestManager().getRequestid();
		long time = System.currentTimeMillis();
		log.info("Start, requestId = " + requestId);
		try {
			process(requestInfo);
			return Action.SUCCESS;
		} catch(Throwable e) {
			User user = requestInfo.getRequestManager().getUser();
			String now = CustomUtil.getStringDate("yyyy-MM-dd HH:mm:ss");
			String ips = CustomUtil.getLocalIP();
			// 精细化处理提示信息
			String msg = "<div style=\"padding-bottom: 10px;\">报错时间：" + now + "，IP组：" + ips + "</div>"
					+ "<div style=\"padding-bottom: 10px;\">请求ID：" + requestId + "，文件：" + getClass().getName() + "，操作人：" + user.getLastname() + "(" + user.getUID() + "）</div>"
					+ "<div>提示消息：" + e.getMessage() + "</div>";
			log.error(msg, e);
			requestInfo.getRequestManager().setMessageid("20088");    // 阻止流程提交（1），消息号，固定该值即可
			requestInfo.getRequestManager().setMessagecontent(msg);    // 阻止流程提交（2），提示消息
		} finally {
			log.info("End, requestId = " + requestId + ", ms: " + (System.currentTimeMillis() - time));
		}
		return Action.FAILURE_AND_CONTINUE;    // 阻止流程提交（3）
	}

	/** Action 业务代码 */
	public abstract void process(RequestInfo requestInfo) throws Exception;

	protected String getTableName(RequestInfo requestInfo) {
		return requestInfo.getRequestManager().getBillTableName();
	}
	
}
